<template>
  <div class="upload-box">
    <el-upload
      list-type="picture-card"
      action=""
      :limit="limit"
      :file-list="fileList"
      :on-preview="handlePictureCardPreview"
      :class="{disabled:fileComputed}"
      :on-remove="handleRemove"
      :on-change="handleChange"
      :http-request="upload"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" width="600px" top="8vh" title="图片预览">
      <img width="100%" :src="dialogImageUrl" alt="">

    </el-dialog>
    <div v-if="showProgress"><el-progress style="width: 180px;" :percentage="percent" /></div>
  </div>

</template>

<script>
import COS from 'cos-js-sdk-v5' // 导入腾讯云的包(sdk)
const cos = new COS({
  SecretId: 'AKIDCXmtNhqniXBe0DRYVKzXXQfrshKm26wX', // 身份识别ID
  SecretKey: 'S2PXUzNYVXSCk4IOJ50ueS6bv63oQs79' // 身份秘钥
})

export default {
  name: 'ImageUpload',
  props: {
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      fileList: [],
      dialogImageUrl: '',
      dialogVisible: false,
      showProgress: false,
      percent: 10
    }
  },
  computed: {
    fileComputed() {
      return this.fileList.length >= this.limit
    }
  },
  methods: {
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
      this.fileList = [...fileList]
      // this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    upload(params) {
      console.log(params)
      const obj = this.fileList.find(item => item.uid === params.file.uid)
      if (params.file) {
        // 执行上传操作
        // this.showProgress = true
        cos.putObject({
          Bucket: 'shenzheng-1302050471', /* 存储桶 */
          Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
          Key: Date.now() + '-' + params.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: params.file, // 上传文件对象
          onProgress: (progressData) => {
            console.log(JSON.stringify(progressData))
            obj.percentage = parseInt(progressData.percent * 100)
            obj.status = 'uploading'
            //  = percent
          }
        }, (err, data) => {
          console.log(err || data)
          if (data.statusCode === 200) {
            // const obj = this.fileList.find(item => item.uid === params.file.uid)
            obj.status = 'success'
            obj.url = 'https://' + data.Location
          }
          // this.showProgress = false
        })
      }
    },
    // file是添加的文件，fileList是添加成功后的文件列表
    handleChange(file, fileList) {
      this.fileList = [...fileList]
    },
    beforeUpload(file) {
      // 1. 限制文件类型 file.type
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传的图片格式, 必须是jpg, gif, bmp, png的格式!')
        return false
      }
      // 2. 限制文件大小 file.size
      if (file.size / 1024 / 1024 >= 5) {
        this.$message.error('上传头像过大, 超过了5M, 必须5M以内')
        return false
      }
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
    .disabled{
        ::v-deep{
            .el-upload--picture-card{
                display: none;
            }
        }
    }
</style>
